3 * Copyright © 2007 Roan Kattouw "<Firstname>.<Lastname>@gmail.com"
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
23 use MediaWiki\MediaWikiServices
;
24 use MediaWiki\Storage\NameTableAccessException
;
25 use MediaWiki\Storage\RevisionRecord
;
28 * Query module to enumerate all deleted revisions.
31 * @deprecated since 1.25
33 class ApiQueryDeletedrevs
extends ApiQueryBase
{
35 public function __construct( ApiQuery
$query, $moduleName ) {
36 parent
::__construct( $query, $moduleName, 'dr' );
39 public function execute() {
40 // Before doing anything at all, let's check permissions
41 $this->checkUserRightsAny( 'deletedhistory' );
43 $this->addDeprecation( 'apiwarn-deprecation-deletedrevs', 'action=query&list=deletedrevs' );
45 $user = $this->getUser();
47 $commentStore = CommentStore
::getStore();
48 $params = $this->extractRequestParams( false );
49 $prop = array_flip( $params['prop'] );
50 $fld_parentid = isset( $prop['parentid'] );
51 $fld_revid = isset( $prop['revid'] );
52 $fld_user = isset( $prop['user'] );
53 $fld_userid = isset( $prop['userid'] );
54 $fld_comment = isset( $prop['comment'] );
55 $fld_parsedcomment = isset( $prop['parsedcomment'] );
56 $fld_minor = isset( $prop['minor'] );
57 $fld_len = isset( $prop['len'] );
58 $fld_sha1 = isset( $prop['sha1'] );
59 $fld_content = isset( $prop['content'] );
60 $fld_token = isset( $prop['token'] );
61 $fld_tags = isset( $prop['tags'] );
63 // If we're in a mode that breaks the same-origin policy, no tokens can
65 if ( $this->lacksSameOriginSecurity() ) {
69 // If user can't undelete, no tokens
70 if ( !$user->isAllowed( 'undelete' ) ) {
74 $result = $this->getResult();
75 $pageSet = $this->getPageSet();
76 $titles = $pageSet->getTitles();
78 // This module operates in three modes:
79 // 'revs': List deleted revs for certain titles (1)
80 // 'user': List deleted revs by a certain user (2)
81 // 'all': List all deleted revs in NS (3)
83 if ( count( $titles ) > 0 ) {
85 } elseif ( !is_null( $params['user'] ) ) {
89 if ( $mode == 'revs' ||
$mode == 'user' ) {
90 // Ignore namespace and unique due to inability to know whether they were purposely set
91 foreach ( [ 'from', 'to', 'prefix', /*'namespace', 'unique'*/ ] as $p ) {
92 if ( !is_null( $params[$p] ) ) {
93 $this->dieWithError( [ 'apierror-deletedrevs-param-not-1-2', $p ], 'badparams' );
97 foreach ( [ 'start', 'end' ] as $p ) {
98 if ( !is_null( $params[$p] ) ) {
99 $this->dieWithError( [ 'apierror-deletedrevs-param-not-3', $p ], 'badparams' );
104 if ( !is_null( $params['user'] ) && !is_null( $params['excludeuser'] ) ) {
105 $this->dieWithError( 'user and excludeuser cannot be used together', 'badparams' );
108 $revisionStore = MediaWikiServices
::getInstance()->getRevisionStore();
109 $arQuery = $revisionStore->getArchiveQueryInfo();
110 $this->addTables( $arQuery['tables'] );
111 $this->addFields( $arQuery['fields'] );
112 $this->addJoinConds( $arQuery['joins'] );
113 $this->addFields( [ 'ar_title', 'ar_namespace' ] );
116 $this->addFields( [ 'ts_tags' => ChangeTags
::makeTagSummarySubquery( 'archive' ) ] );
119 if ( !is_null( $params['tag'] ) ) {
120 $this->addTables( 'change_tag' );
122 [ 'change_tag' => [ 'JOIN', [ 'ar_rev_id=ct_rev_id' ] ] ]
124 $changeTagDefStore = MediaWikiServices
::getInstance()->getChangeTagDefStore();
126 $this->addWhereFld( 'ct_tag_id', $changeTagDefStore->getId( $params['tag'] ) );
127 } catch ( NameTableAccessException
$exception ) {
129 $this->addWhere( '1=0' );
133 // This means stricter restrictions
134 if ( $fld_content ) {
135 $this->checkUserRightsAny( [ 'deletedtext', 'undelete' ] );
138 $userMax = $fld_content ? ApiBase
::LIMIT_SML1
: ApiBase
::LIMIT_BIG1
;
139 $botMax = $fld_content ? ApiBase
::LIMIT_SML2
: ApiBase
::LIMIT_BIG2
;
141 $limit = $params['limit'];
143 if ( $limit == 'max' ) {
144 $limit = $this->getMain()->canApiHighLimits() ?
$botMax : $userMax;
145 $this->getResult()->addParsedLimit( $this->getModuleName(), $limit );
148 $this->validateLimit( 'limit', $limit, 1, $userMax, $botMax );
151 // Undelete tokens are identical for all pages, so we cache one here
152 $token = $user->getEditToken( '', $this->getMain()->getRequest() );
155 $dir = $params['dir'];
157 // We need a custom WHERE clause that matches all titles.
158 if ( $mode == 'revs' ) {
159 $lb = new LinkBatch( $titles );
160 $where = $lb->constructSet( 'ar', $db );
161 $this->addWhere( $where );
162 } elseif ( $mode == 'all' ) {
163 $this->addWhereFld( 'ar_namespace', $params['namespace'] );
165 $from = $params['from'] === null
167 : $this->titlePartToKey( $params['from'], $params['namespace'] );
168 $to = $params['to'] === null
170 : $this->titlePartToKey( $params['to'], $params['namespace'] );
171 $this->addWhereRange( 'ar_title', $dir, $from, $to );
173 if ( isset( $params['prefix'] ) ) {
174 $this->addWhere( 'ar_title' . $db->buildLike(
175 $this->titlePartToKey( $params['prefix'], $params['namespace'] ),
176 $db->anyString() ) );
180 if ( !is_null( $params['user'] ) ) {
181 // Don't query by user ID here, it might be able to use the ar_usertext_timestamp index.
182 $actorQuery = ActorMigration
::newMigration()
183 ->getWhere( $db, 'ar_user', User
::newFromName( $params['user'], false ), false );
184 $this->addTables( $actorQuery['tables'] );
185 $this->addJoinConds( $actorQuery['joins'] );
186 $this->addWhere( $actorQuery['conds'] );
187 } elseif ( !is_null( $params['excludeuser'] ) ) {
188 // Here there's no chance of using ar_usertext_timestamp.
189 $actorQuery = ActorMigration
::newMigration()
190 ->getWhere( $db, 'ar_user', User
::newFromName( $params['excludeuser'], false ) );
191 $this->addTables( $actorQuery['tables'] );
192 $this->addJoinConds( $actorQuery['joins'] );
193 $this->addWhere( 'NOT(' . $actorQuery['conds'] . ')' );
196 if ( !is_null( $params['user'] ) ||
!is_null( $params['excludeuser'] ) ) {
197 // Paranoia: avoid brute force searches (T19342)
198 // (shouldn't be able to get here without 'deletedhistory', but
199 // check it again just in case)
200 if ( !$user->isAllowed( 'deletedhistory' ) ) {
201 $bitmask = RevisionRecord
::DELETED_USER
;
202 } elseif ( !$user->isAllowedAny( 'suppressrevision', 'viewsuppressed' ) ) {
203 $bitmask = RevisionRecord
::DELETED_USER | RevisionRecord
::DELETED_RESTRICTED
;
208 $this->addWhere( $db->bitAnd( 'ar_deleted', $bitmask ) . " != $bitmask" );
212 if ( !is_null( $params['continue'] ) ) {
213 $cont = explode( '|', $params['continue'] );
214 $op = ( $dir == 'newer' ?
'>' : '<' );
215 if ( $mode == 'all' ||
$mode == 'revs' ) {
216 $this->dieContinueUsageIf( count( $cont ) != 4 );
218 $this->dieContinueUsageIf( strval( $ns ) !== $cont[0] );
219 $title = $db->addQuotes( $cont[1] );
220 $ts = $db->addQuotes( $db->timestamp( $cont[2] ) );
221 $ar_id = (int)$cont[3];
222 $this->dieContinueUsageIf( strval( $ar_id ) !== $cont[3] );
223 $this->addWhere( "ar_namespace $op $ns OR " .
224 "(ar_namespace = $ns AND " .
225 "(ar_title $op $title OR " .
226 "(ar_title = $title AND " .
227 "(ar_timestamp $op $ts OR " .
228 "(ar_timestamp = $ts AND " .
229 "ar_id $op= $ar_id)))))" );
231 $this->dieContinueUsageIf( count( $cont ) != 2 );
232 $ts = $db->addQuotes( $db->timestamp( $cont[0] ) );
233 $ar_id = (int)$cont[1];
234 $this->dieContinueUsageIf( strval( $ar_id ) !== $cont[1] );
235 $this->addWhere( "ar_timestamp $op $ts OR " .
236 "(ar_timestamp = $ts AND " .
237 "ar_id $op= $ar_id)" );
241 $this->addOption( 'LIMIT', $limit +
1 );
242 if ( $mode == 'all' ) {
243 if ( $params['unique'] ) {
244 // @todo Does this work on non-MySQL?
245 $this->addOption( 'GROUP BY', 'ar_title' );
247 $sort = ( $dir == 'newer' ?
'' : ' DESC' );
248 $this->addOption( 'ORDER BY', [
250 'ar_timestamp' . $sort,
255 if ( $mode == 'revs' ) {
256 // Sort by ns and title in the same order as timestamp for efficiency
257 $this->addWhereRange( 'ar_namespace', $dir, null, null );
258 $this->addWhereRange( 'ar_title', $dir, null, null );
260 $this->addTimestampWhereRange( 'ar_timestamp', $dir, $params['start'], $params['end'] );
261 // Include in ORDER BY for uniqueness
262 $this->addWhereRange( 'ar_id', $dir, null, null );
264 $res = $this->select( __METHOD__
);
265 $pageMap = []; // Maps ns&title to (fake) pageid
268 foreach ( $res as $row ) {
269 if ( ++
$count > $limit ) {
271 if ( $mode == 'all' ||
$mode == 'revs' ) {
272 $this->setContinueEnumParameter( 'continue',
273 "$row->ar_namespace|$row->ar_title|$row->ar_timestamp|$row->ar_id"
276 $this->setContinueEnumParameter( 'continue', "$row->ar_timestamp|$row->ar_id" );
284 $rev['timestamp'] = wfTimestamp( TS_ISO_8601
, $row->ar_timestamp
);
286 $rev['revid'] = (int)$row->ar_rev_id
;
288 if ( $fld_parentid && !is_null( $row->ar_parent_id
) ) {
289 $rev['parentid'] = (int)$row->ar_parent_id
;
291 if ( $fld_user ||
$fld_userid ) {
292 if ( $row->ar_deleted
& RevisionRecord
::DELETED_USER
) {
293 $rev['userhidden'] = true;
296 if ( Revision
::userCanBitfield( $row->ar_deleted
, RevisionRecord
::DELETED_USER
, $user ) ) {
298 $rev['user'] = $row->ar_user_text
;
301 $rev['userid'] = (int)$row->ar_user
;
306 if ( $fld_comment ||
$fld_parsedcomment ) {
307 if ( $row->ar_deleted
& RevisionRecord
::DELETED_COMMENT
) {
308 $rev['commenthidden'] = true;
311 if ( Revision
::userCanBitfield( $row->ar_deleted
, RevisionRecord
::DELETED_COMMENT
, $user ) ) {
312 $comment = $commentStore->getComment( 'ar_comment', $row )->text
;
313 if ( $fld_comment ) {
314 $rev['comment'] = $comment;
316 if ( $fld_parsedcomment ) {
317 $title = Title
::makeTitle( $row->ar_namespace
, $row->ar_title
);
318 $rev['parsedcomment'] = Linker
::formatComment( $comment, $title );
324 $rev['minor'] = $row->ar_minor_edit
== 1;
327 $rev['len'] = $row->ar_len
;
330 if ( $row->ar_deleted
& RevisionRecord
::DELETED_TEXT
) {
331 $rev['sha1hidden'] = true;
334 if ( Revision
::userCanBitfield( $row->ar_deleted
, RevisionRecord
::DELETED_TEXT
, $user ) ) {
335 if ( $row->ar_sha1
!= '' ) {
336 $rev['sha1'] = Wikimedia\base_convert
( $row->ar_sha1
, 36, 16, 40 );
342 if ( $fld_content ) {
343 if ( $row->ar_deleted
& RevisionRecord
::DELETED_TEXT
) {
344 $rev['texthidden'] = true;
347 if ( Revision
::userCanBitfield( $row->ar_deleted
, RevisionRecord
::DELETED_TEXT
, $user ) ) {
348 ApiResult
::setContentValue( $rev, 'text', Revision
::getRevisionText( $row, 'ar_' ) );
353 if ( $row->ts_tags
) {
354 $tags = explode( ',', $row->ts_tags
);
355 ApiResult
::setIndexedTagName( $tags, 'tag' );
356 $rev['tags'] = $tags;
362 if ( $anyHidden && ( $row->ar_deleted
& RevisionRecord
::DELETED_RESTRICTED
) ) {
363 $rev['suppressed'] = true;
366 if ( !isset( $pageMap[$row->ar_namespace
][$row->ar_title
] ) ) {
367 $pageID = $newPageID++
;
368 $pageMap[$row->ar_namespace
][$row->ar_title
] = $pageID;
369 $a['revisions'] = [ $rev ];
370 ApiResult
::setIndexedTagName( $a['revisions'], 'rev' );
371 $title = Title
::makeTitle( $row->ar_namespace
, $row->ar_title
);
372 ApiQueryBase
::addTitleInfo( $a, $title );
374 $a['token'] = $token;
376 $fit = $result->addValue( [ 'query', $this->getModuleName() ], $pageID, $a );
378 $pageID = $pageMap[$row->ar_namespace
][$row->ar_title
];
379 $fit = $result->addValue(
380 [ 'query', $this->getModuleName(), $pageID, 'revisions' ],
384 if ( $mode == 'all' ||
$mode == 'revs' ) {
385 $this->setContinueEnumParameter( 'continue',
386 "$row->ar_namespace|$row->ar_title|$row->ar_timestamp|$row->ar_id"
389 $this->setContinueEnumParameter( 'continue', "$row->ar_timestamp|$row->ar_id" );
394 $result->addIndexedTagName( [ 'query', $this->getModuleName() ], 'page' );
397 public function isDeprecated() {
401 public function getAllowedParams() {
404 ApiBase
::PARAM_TYPE
=> 'timestamp',
405 ApiBase
::PARAM_HELP_MSG_INFO
=> [ [ 'modes', 1, 2 ] ],
408 ApiBase
::PARAM_TYPE
=> 'timestamp',
409 ApiBase
::PARAM_HELP_MSG_INFO
=> [ [ 'modes', 1, 2 ] ],
412 ApiBase
::PARAM_TYPE
=> [
416 ApiBase
::PARAM_DFLT
=> 'older',
417 ApiBase
::PARAM_HELP_MSG
=> 'api-help-param-direction',
418 ApiBase
::PARAM_HELP_MSG_INFO
=> [ [ 'modes', 1, 3 ] ],
421 ApiBase
::PARAM_HELP_MSG_INFO
=> [ [ 'modes', 3 ] ],
424 ApiBase
::PARAM_HELP_MSG_INFO
=> [ [ 'modes', 3 ] ],
427 ApiBase
::PARAM_HELP_MSG_INFO
=> [ [ 'modes', 3 ] ],
430 ApiBase
::PARAM_DFLT
=> false,
431 ApiBase
::PARAM_HELP_MSG_INFO
=> [ [ 'modes', 3 ] ],
434 ApiBase
::PARAM_TYPE
=> 'namespace',
435 ApiBase
::PARAM_DFLT
=> NS_MAIN
,
436 ApiBase
::PARAM_HELP_MSG_INFO
=> [ [ 'modes', 3 ] ],
440 ApiBase
::PARAM_TYPE
=> 'user'
443 ApiBase
::PARAM_TYPE
=> 'user'
446 ApiBase
::PARAM_DFLT
=> 'user|comment',
447 ApiBase
::PARAM_TYPE
=> [
461 ApiBase
::PARAM_ISMULTI
=> true
464 ApiBase
::PARAM_DFLT
=> 10,
465 ApiBase
::PARAM_TYPE
=> 'limit',
466 ApiBase
::PARAM_MIN
=> 1,
467 ApiBase
::PARAM_MAX
=> ApiBase
::LIMIT_BIG1
,
468 ApiBase
::PARAM_MAX2
=> ApiBase
::LIMIT_BIG2
471 ApiBase
::PARAM_HELP_MSG
=> 'api-help-param-continue',
476 protected function getExamplesMessages() {
478 'action=query&list=deletedrevs&titles=Main%20Page|Talk:Main%20Page&' .
479 'drprop=user|comment|content'
480 => 'apihelp-query+deletedrevs-example-mode1',
481 'action=query&list=deletedrevs&druser=Bob&drlimit=50'
482 => 'apihelp-query+deletedrevs-example-mode2',
483 'action=query&list=deletedrevs&drdir=newer&drlimit=50'
484 => 'apihelp-query+deletedrevs-example-mode3-main',
485 'action=query&list=deletedrevs&drdir=newer&drlimit=50&drnamespace=1&drunique='
486 => 'apihelp-query+deletedrevs-example-mode3-talk',
490 public function getHelpUrls() {
491 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Deletedrevs';